//
//  HSYBaseNavigationController+UI.m
//  高仿暴走斗图
//
//  Created by key on 16/4/18.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "HSYBaseNavigationController+UI.h"
#import "HSYBaseNavigationController+Animation.h"

@implementation HSYBaseNavigationController (UI)


#pragma mark - Add Shadow

- (void)updateShadowForCenterView {
    
    UIView * centerView = self.view;
    if(self.showsShadow){
        
        [HSYBaseNavigationController showShadowInContentView:centerView];
        
    } else if (centerView.layer.shadowPath != NULL) {
        
        centerView.layer.shadowRadius = 0.f;
        centerView.layer.shadowOpacity = 0.f;
        centerView.layer.shadowOffset = CGSizeMake(0, -3);
        centerView.layer.shadowPath = NULL;
        centerView.layer.masksToBounds = YES;
    }
}

+ (void)showShadowInContentView:(UIView *)contentView {
    
    contentView.layer.masksToBounds = NO;
    contentView.layer.shadowRadius = 10.0f;
    contentView.layer.shadowOpacity = 0.8f;
    contentView.layer.shadowOffset = CGSizeMake(0, -3);
    contentView.layer.shadowColor = [[UIColor blackColor] CGColor];
    
    if (contentView.layer.shadowPath == NULL) {
        
        contentView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:contentView.bounds] CGPath];
        
    } else {
        
        CGRect currentPath = CGPathGetPathBoundingBox(contentView.layer.shadowPath);
        
        if (CGRectEqualToRect(currentPath, contentView.bounds) == NO) {
            
            contentView.layer.shadowPath = [[UIBezierPath bezierPathWithRect:contentView.bounds] CGPath];
        }
    }
}

#pragma mark - Move Type

- (void)addPOPInterface:(CGRect)frame {
    
    if (self.backgroundView) {
        self.backgroundView.hidden = NO;
        return;
    }
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(self.view.viewX, 0, CGRectGetWidth(frame), CGRectGetHeight(frame))];
    [self.view.superview insertSubview:self.backgroundView belowSubview:self.view];
    
    if (self.blackMask) {
        [self.blackMask removeFromSuperview];
        self.blackMask = nil;
    }
    self.blackMask = [[UIView alloc] initWithFrame:self.backgroundView.bounds];
    self.blackMask.backgroundColor = CLEAR_COLOR;
    [self.backgroundView addSubview:self.blackMask];
}

- (void)createScreenshotImageWithTransitionsAnimationType:(kHSYTransitionsAnimationType)type {
    
    switch (type) {
        case kHSYTransitionsAnimationTypeRotation: {
            
            
        }
            break;
            
        case kHSYTransitionsAnimationTypeCard: {
            
            [self cardAnimation_CreateLastScreenImage];
        }
            break;
        
        case kHSYTransitionsAnimationTypeMove: {
         
            [self moveAnimation_CreateLastScreenImage];
        }
            break;
            
        default:
            break;
    }
}

- (void)moveAnimation_CreateLastScreenImage {
    
    [self getLastScreenshotView];
    self.startBackViewX = CGRectGetMidX(self.view.frame) + PROPORTION;
    
    self.lastScreenShotView.frame = CGRectMake(self.startBackViewX, self.lastScreenShotView.viewY, self.lastScreenShotView.viewHeight, self.lastScreenShotView.viewWidth);
    [self.backgroundView insertSubview:self.lastScreenShotView belowSubview:self.blackMask];
}

- (void)cardAnimation_CreateLastScreenImage {
    
    [self getLastScreenshotView];
    CGFloat offset = CARD_ANIMATION_OFFSET;
    self.lastScreenShotView.frame = CGRectMake(offset, offset, IPHONE_WIDTH - offset*2, IPHONE_HEIGHT - offset*2);
    [self.backgroundView insertSubview:self.lastScreenShotView belowSubview:self.blackMask];
}


- (void)getLastScreenshotView {
    
    if (self.lastScreenShotView) {
        [self.lastScreenShotView removeFromSuperview];
        self.lastScreenShotView = nil;
    }
    UIImage *lastScreenImg = [self.screenViewControllers objectAtIndex:self.viewControllers.count - 2];
    self.lastScreenShotView = [[UIImageView alloc]initWithImage:lastScreenImg];
}



@end
